#!/bin/bash
SERVICE_PORT=8080
SERVICE_DIR=/root/jenkins/workspace/mall-admin/target/
SERVICE_NAME=mall-admin-1.0.jar
SERVICE_LOG=mall.log

function killproject()
{
  project_pid=$(netstat -lnp | grep $SERVICE_PORT|awk '{print $7}'|cut -d/ -f1)
  if [  $project_pid > 0 ]; then
        echo $SERVICE_NAME "已经启动，正在关闭项目，pid为: $project_pid "
        kill -9 $(netstat -lnp | grep $SERVICE_PORT|awk '{print $7}'|cut -d/ -f1)
        echo $SERVICE_NAME '关闭成功，开始重启项目。。。'
  else
        echo $SERVICE_NAME "没有启动，直接启动项目。。。"
  fi
}
function start_project()
{
        source /etc/profile
        echo $SERVICE_NAME '正在启动。。。'
        cd $SERVICE_DIR
        nohup java -server -Xms256m -Xmx256m -jar $SERVICE_NAME --spring.profiles.active=prod > $SERVICE_LOG 2>&1 &

}
function check_project()
{
  check_pid=$(netstat -lnp | grep $SERVICE_PORT|awk '{print $7}'|cut -d/ -f1)
  if [ $check_pid  > 0 ]; then
        echo $SERVICE_NAME "启动成功, pid为: $check_pid  "
  else
        echo $SERVICE_NAME "还未启动"
  fi

}
killproject
start_project
sleep 5
#check_project
